package com.pingba.serviceImpl.oauth;

import com.pingba.convert.OauthConvert;
import com.pingba.dao.BaseDao;
import com.pingba.dao.UserDao;
import com.pingba.dao.oauth.WeCharUserDao;
import com.pingba.entity.User;
import com.pingba.entity.oauth.WeCharUser;
import com.pingba.service.UserService;
import com.pingba.service.oauth.WeCharUserService;
import com.pingba.serviceImpl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by Administrator on 2015/12/9 0009.
 */
@Service("WeCharUserService")
public class WeCharUserServiceImpl extends BaseServiceImpl<WeCharUser, Long> implements WeCharUserService {

    @Autowired
    private WeCharUserDao weCharUserDao;

    @Autowired
    private UserService userService;

    @Override
    public BaseDao<WeCharUser, Long> getDao() {
        return weCharUserDao;
    }

    /**
     * @see com.pingba.service.oauth.WeCharUserService#login(WeCharUser)
     * @param weCharUser
     * @return
     */
    @Override
    public User login(WeCharUser weCharUser) {
        WeCharUser userFind = (WeCharUser) weCharUserDao.selectOne(weCharUser);
        if (userFind == null) {
            weCharUser.setRegDate(new Date());
            weCharUserDao.insert(weCharUser);
            User user = OauthConvert.fromWechar(weCharUser);
            user.setPhone("111111");
            user.setPasswd("11111");
            userService.insert(user);
            return user;
        }
        User user =  userService.findByWeCharUnionId(weCharUser.getUnionId());
        return user;
    }
}
